<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@taglib uri="/WEB-INF/tld/pageutil.tld" prefix="pu"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>

<c-rt:if test="${!empty param.questionID || !empty param.id}">
	<c-rt:if test="${!empty param.questionID}">
		<c-rt:set var="questionID">${param.questionID}</c-rt:set>
	</c-rt:if> 
	<c-rt:if test="${!empty param.id}">
		<c-rt:set var="questionID">${param.id}</c-rt:set>
	</c-rt:if>
	
	<c-rt:set var="question" value="${pu:get('com.yaodian100.crm.cs.entity.Question', questionID)}"/>
</c-rt:if>
<c-rt:if test="${!empty question}">
${pu:reqSet('memberId', question.memberId) }
${pu:reqSet('orderId', question.extraValue['orderId'].value) }
${pu:reqSet('isProductQAA', question.extraValue['isProductQAA'].value) }
${pu:reqSet('returnId', question.extraValue['returnId'].value) }
${pu:reqSet('changeId', question.extraValue['changeId'].value) }
${pu:reqSet('channel', question.channel.description) }

${pu:reqSet('phone', question.extraValue['phone'].value) }
${pu:reqSet('phoneArea', question.extraValue['phoneArea'].value) }
${pu:reqSet('phoneExt', question.extraValue['phoneExt'].value) }
${pu:reqSet('mobile', question.extraValue['mobile'].value) }
${pu:reqSet('email', question.extraValue['email'].value) }
${pu:reqSet('productId', question.extraValue['productId'].value) }
</c-rt:if>
<c-rt:if test="${empty question}">
${pu:reqSet('memberId', param.memberId) }
${pu:reqSet('orderId', param.orderId) }
${pu:reqSet('isProductQAA', param.isProductQAA) }
${pu:reqSet('returnId', param.returnId) }
${pu:reqSet('changeId', param.changeId) }
${pu:reqSet('channel', param.channel) }
${pu:reqSet('productId', param.productId) }

<c-rt:if test="${!empty param.memberId}">
${pu:reqSet('member', pu:get('com.yaodian100.ec.member.entity.Member', param.memberId)) }
${pu:reqSet('phone', member.homePhone)}
${pu:reqSet('phoneArea', '') }
${pu:reqSet('phoneExt', '') }
${pu:reqSet('mobile', member.mobile) }
${pu:reqSet('email', member.email) }
</c-rt:if>
</c-rt:if>
<c-rt:if test="${!empty param.questionType}">
<c-rt:set var="questionType" value="${param.questionType }"/>
</c-rt:if>
<c-rt:if test="${empty param.questionType}">
<c-rt:set var="questionType" value="${question.type.name }"/>
</c-rt:if>
<c-rt:if test="${!empty question}">
	<c-rt:set var="qt" value="${question.type}"/>
</c-rt:if>
<c-rt:if test="${empty question}">
	<c-rt:set var="qt" value="${pu:get('com.yaodian100.crm.cs.entity.QuestionType', questionType)}"/>
</c-rt:if>

<c-rt:set var="fromChannels" value="${pu:queryByHql('from FromChannel', null, null)}"/>
	<script language="javascript">
	$(document).ready( function() {
		$('.submit_button').click(function(){
			message='';
			if(document.theForm.fromChannel.value==''){
				message+='请选择channel\r\n';
			}
			if(document.theForm.memberId.value.length>10){
				message+='会员编号长度最多为10个字\r\n';
			}
			
			if(message==''){
				doAR('<%=application.getContextPath() %>/cs/updateQuestion.do', 'main', 'theForm');
			}else{
				alert(message);
			}
		});
	});
	
	</script>

<br/>
<h1>
<c-rt:choose>
<c-rt:when test="${empty question || param.forward=='newOrder'}">
新增
</c-rt:when>
<c-rt:otherwise>
修改
</c-rt:otherwise>
</c-rt:choose>
${qt.description }</h1>
<c-rt:if test="${!empty param.operation}">
<c-rt:if test="${empty question}">
已储存！
</c-rt:if>
<c-rt:if test="${!empty question}">
已储存修改！
</c-rt:if>
</c-rt:if>
<form method="post" id="theForm" name="theForm">
<input type="hidden" name="questionType" value="${questionType}"/>
<c-rt:if test="${empty question}">
<input type="hidden" name="operation" value="save"/>
</c-rt:if>
<c-rt:if test="${!empty question}">
<input type="hidden" name="operation" value="update"/>
</c-rt:if>
<table class="tb-data" width="100%">
<c-rt:choose>
<c-rt:when test="${param.forward=='newOrder'}">
<tr>
<th>前问题单</th><td>${question.id}<input type="hidden" name="orgQuestionId" value="${question.id }"/></td>
</tr>
<tr>
<th width="130">questionID</th><td><input type="hidden" name="id" value=""/></td>
  </tr>
</c-rt:when>
<c-rt:otherwise>
<tr>
<th width="130">questionID</th><td>${question.id}<input type="hidden" name="id" value="${question.id }"/></td>
  </tr>
</c-rt:otherwise>
</c-rt:choose>

<tr>
  <th>来源</th>
  <td><select name="fromChannel">
    <option value="">请选择</option>
    <c-rt:forEach items="${fromChannels }" var="fc">
      <option value="${fc.id }" ${fc.description==channel?'selected="selected"':''}>${fc.title }</option>
    </c-rt:forEach>
  </select></td>
</tr>
<tr>
  <th>会员编号</th>
<td><input type="text" id="memberId" name="memberId" value="${memberId }"/>
  <%--
<c-rt:if test="${!empty question.memberId}">
<c-rt:set var="member" value="${pu:get('com.yaodian100.ec.member.entity.Member', question.memberId)}"/>
</c-rt:if>
<input type="text" id="member" name="member" value="${member.name }" readonly="readonly"/> 
--%>
  <%--
<a href="javascript:doAR('<%=application.getContextPath() %>/cs/page.do?iamlazy=memberList&AJAX=true&refreshZone=dialog&returnFieldIds=memberId,member','dialog');openDialog('select')">选择</a>
--%></td>
</tr>
<tr>
  <th>短描述</th>
  <td><input type="text" name="shortDesc" value="${question.shortDesc }"/></td>
  </tr>
<tr>
  <th>客户问题说明</th>
  <td><textarea name="description">${question.description }</textarea></td>
</tr>
<tr>
  <th width="130">&nbsp;</th><td>&nbsp;</td></tr>

<c-rt:forEach items="${qt.extraAttr.attributes }" var="attr" varStatus="status">
<c-rt:if test="${attr.dataType!='Button'}">
	<tr>
		<th>${attr.description }
		<c-rt:if test="${attr.dataType=='Component'}">
				<c-rt:if test="${attr.name=='class1'}">
				<script>
					$(function(){
						doAR('${attr.componentUrl}&class1=${question.extraValue[attr.name].value}&questionId=${question.id}', '${attr.name }Zone');
					});
				</script>
				</c-rt:if>
				<c-rt:if test="${attr.name=='class2'}">
				<script>
					$(function(){
						doAR('${attr.componentUrl}&class2=${question.extraValue[attr.name].value}&parentId=${question.extraValue["class1"].value}', '${attr.name }Zone');
					});
				</script>
				</c-rt:if>
				<c-rt:if test="${attr.name=='commonQuestionType'}">
				<script>
					var aform;
					$(function(){
						doAR('${attr.componentUrl}&commonQuestionType=${question.extraValue[attr.name].value}&parentId=${question.extraValue["class2"].value}', '${attr.name }Zone',aform, refreshGroup);
					});
				</script>
				</c-rt:if>
				<c-rt:if test="${attr.name=='mustInquire'}">
				
				<script>
					$(function(){
						doAR("${attr.componentUrl}&cqtId=${question.extraValue['commonQuestionType'].value}&mustInquire=${question.extraValue[attr.name].value}", '${attr.name }Zone');
					});
				</script>
				</c-rt:if>

		</c-rt:if>
		</th>
		
		<c-rt:if test="${attr.dataType=='Text'}">
			<td>
				<c-rt:if test="${attr.name!='productId' && attr.name!='orderId' && attr.name!='isProductQAA' && attr.name!='returnId' && 
								 attr.name!='changeId' && attr.name!='mobile' && attr.name!='phoneArea' &&
								 attr.name!='phone' && attr.name!='phoneExt' && attr.name!='email'}">
				<input type="text" name="${attr.name}" value="${question.extraValue[attr.name].value }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='orderId'}">
				<input type="text" name="${attr.name}" value="${orderId}"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='isProductQAA'}">
				<input type="text" name="${attr.name }" value="${isProductQAA }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='returnId'}">
				<input type="text" name="${attr.name }" value="${returnId }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='changeId'}">
				<input type="text" name="${attr.name }" value="${changeId }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='mobile'}">
				<input type="text" name="${attr.name }" value="${mobile }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='phoneArea'}">
				<input type="text" name="${attr.name }" value="${phoneArea }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='phone'}">
				<input type="text" name="${attr.name }" value="${phone }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='phoneExt'}">
				<input type="text" name="${attr.name }" value="${phoneExt }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='email'}">
				<input type="text" name="${attr.name }" value="${email }"/>
				</c-rt:if>
				<c-rt:if test="${attr.name=='productId'}">
				<input type="text" name="${attr.name }" value="${productId }"/>
				</c-rt:if>
				
				${attr.tip }
			</td>
		</c-rt:if>
		<c-rt:if test="${attr.dataType=='Select'}">
			<td>
				<select name="${attr.name}">
				${pu:reqSet('presentNames', pu:split(attr.presentNames, ',')) }
				<c-rt:forEach items="${pu:split(attr.presentValues, ',')}" var="value" varStatus="valueStatus">
					<option value="${value }" <c-rt:if test="${question.extraValue[attr.name].value==value}">selected="selected"</c-rt:if>>${presentNames[valueStatus.index] }</option>
				</c-rt:forEach>
				</select>
			</td>
		</c-rt:if>
		<c-rt:if test="${attr.dataType=='Component'}">
			<td id="${attr.name }Zone">
				
			</td>
		</c-rt:if>
		<c-rt:if test="${attr.dataType=='Radio'}">
			<td>
				${question.extraValue[attr.name].value}
					<c-rt:forEach items="${pu:split(attr.presentValues, ',')}" var="value">
						<input type="radio" name="${attr.name }" value="${value }" <c-rt:if test="${question.extraValue[attr.name].value==value}">checked="checked"</c-rt:if>/>${value }
					</c-rt:forEach>

			</td>
		</c-rt:if>
	</tr>
	</c-rt:if>
</c-rt:forEach>
<tr><th>处理等级</th><td>
			<select name="priority">
				<option value="0">函件</option>
				<option value="1">一般件</option>
				<option value="2">急件</option>
				<option value="3">特急件</option>
			</select>
		</td></tr>
<c-rt:if test="${!empty param.formType}">
<tr>
	<th>立单原因描述
	<script>
	function submitForm(responser, workFlowNodeId){
		var error="";
		if(document.theForm.fromChannel.value==''){
			error+="请选择来源\r\n";
		}
		if(document.theForm.description.value=='' && document.theForm.comment.value==''){
			error+='请填客户问题说明或立单原因描述\r\n';
		}
		if(document.theForm.memberId.value.length>10){
			error+='会员编号长度最多为10个字\r\n';
		}
		<c-rt:forEach items="${qt.extraAttr.attributes }" var="attr">
		<c-rt:if test="${attr.mandatory}">
			if (document.theForm.${attr.name}.value==''){
				error+='${attr.description}为必填\r\n';
			}
		</c-rt:if>
	</c-rt:forEach>
		if(error!=''){
			alert(error);
		}else{
			document.theForm.responser.value=responser;
			document.theForm.workFlowNodeId.value=workFlowNodeId;
			doAR('<%=application.getContextPath() %>/cs/createOrderAndQuestion.do', 'main', 'theForm');
		}
	}
	function submitForwardForm(type, responser, workFlowNodeId){
		var error="";
		if(document.theForm.fromChannel.value==''){
			error+="请选择来源\r\n";
		}
		if(document.theForm.memberId.value.length>10){
			error+='会员编号长度最多为10个字\r\n';
		}
		if(error!=''){
			alert(error);
		}else{
			doAR('<%=application.getContextPath() %>/cs/createOrderAndQuestion.do?responser='+responser+'&workFlowNodeId='+workFlowNodeId, 'main', 'theForm');
		}
	}
	</script>
	
	</th>
	<td>
	<textarea name="comment">${question.comment }</textarea>
	</td>
</tr>
<tr>
	<th>转单</th><td id="forwardOrderZone">
<c-rt:if test="${questionType!='AskDeliver'}">
<c-rt:if test="${!empty qt.workFlowName}">
		<c-rt:set var="params" value="${pu:addToList('paras', qt.workFlowName)}"/>
		<c-rt:set var="hql">
		from WorkFlowNode where workFlow.name=? and parent is null
		</c-rt:set>
		<c-rt:set var="workFlowNodes" value="${pu:queryByHql(hql, params, null)}"/>
</c-rt:if>
		<c-rt:if test="${empty workFlowNodes}">

		</c-rt:if>
		<input type="hidden" name="responser"/>
		<input type="hidden" name="workFlowNodeId"/>
		<c-rt:set var="tempShortDesc" value=""/>
		<c-rt:forEach items="${workFlowNodes}" var="workFlowNode">
			<c-rt:if test="${not fn:contains(tempShortDesc,workFlowNode.shortDesc)}">
				<c-rt:set var="tempShortDesc" value="${tempShortDesc},${workFlowNode.shortDesc}"/>
				<input type="button" value="${workFlowNode.shortDesc }" onclick="submitForm('${workFlowNode.responser}', '${workFlowNode.id}')"/>
			</c-rt:if>
		<%--
		<a href="javascript:;" onclick="openDialog('转单', buildForwardForm('${workOrder.id}', '${workFlowNode.id }', '${workFlowNode.responser }', '${workFlowNode.shortDesc }'))">${workFlowNode.shortDesc }</a>
		--%>
		</c-rt:forEach>
</c-rt:if>
<c-rt:if test="${questionType=='AskDeliver'}">

<c-rt:if test="${!empty param.questionType}">
<c-rt:set var="params" value="${pu:addToList('paras', param.questionType)}"/>
		<c-rt:set var="hql">
		from WorkFlowNode where workFlow.name=? and parent is null
		</c-rt:set>
		<c-rt:set var="workFlowNodes" value="${pu:queryByHql(hql, params, null)}"/>
</c-rt:if>
		<c-rt:if test="${empty workFlowNodes}">

		</c-rt:if>
		<input type="hidden" name="responser"/>
		<input type="hidden" name="workFlowNodeId"/>
		<c-rt:if test="${!empty param.orderId}">
${pu:listAdd('orderParams', param.orderId) }
${pu:reqSet('orderProducts', pu:queryByHql('from OrderProduct where orderId=?', pu:arrayGet('orderParams'), null))}
<c-rt:set var="tempLogisticProvider" value=""/>
<c-rt:set var="tempShortDesc" value=""/>
<c-rt:forEach items="${orderProducts}" var="op">
	${pu:reqSet('lp', pu:get('com.yaodian100.crm.workorder.entity.LogisticProvider', op.logisticCompany)) }
	<c-rt:if test="${lp.isProcess=='Y'}">
		<c-rt:forEach items="${workFlowNodes}" var="workFlowNode">
			<c-rt:if test="${not fn:contains(tempLogisticProvider,lp.name)}">
				<c-rt:set var="tempLogisticProvider" value="${tempLogisticProvider},${lp.name}"/>
				<input type="button" value="${lp.name }" onclick="submitForm('L.${lp.id }', '${workFlowNode.id}')"/>
			</c-rt:if>	
		</c-rt:forEach>
	</c-rt:if>
	<c-rt:if test="${lp.isProcess=='N'}">
	<c-rt:forEach items="${workFlowNodes}" var="workFlowNode">
		<c-rt:if test="${not fn:contains(tempShortDesc,workFlowNode.shortDesc)}">
			<c-rt:set var="tempShortDesc" value="${tempShortDesc},${workFlowNode.shortDesc}"/>
			<input type="button" value="${workFlowNode.shortDesc }" onclick="submitForm('${workFlowNode.responser}', '${workFlowNode.id}')"/>
		</c-rt:if>	
		</c-rt:forEach>
	</c-rt:if>
</c-rt:forEach>
</c-rt:if>
</c-rt:if>
	</td>
	</tr>
</c-rt:if>
<c-rt:if test="${empty param.formType}">	
	<tr>
	<th></th><td><input type="button" class="submit_button" value="送出"/>
	<input type="reset" value="重设"/>
	</td>
	</tr>
</c-rt:if>	
	</table>
</form>
<script>
function refreshGroup(){
	if(document.theForm.commonQuestionType.value!=''){
		doAR('<%=application.getContextPath() %>/cs/page.do?iamlazy=forwardOrderZone&cqtId='+document.theForm.commonQuestionType.value, 'forwardOrderZone');
		
	}
}
</script>
